{#
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
#}

{% extends "airflow/task_instance.html" %}
{% block title %}Airflow - DAGs{% endblock %}

{% block body %}
{{ super() }}
<h4>{{ title }}</h4>
<ul class="nav nav-pills" role="tablist">
  {% for log in logs %}
  <li role="presentation" class="{{ 'active' if loop.last else '' }}">
    <a href="#{{ loop.index }}" aria-controls="{{ loop.index }}" role="tab" data-toggle="tab">
      {{ loop.index }}
    </a>
  </li>
  {% endfor %}
  <li class="active" style="float: right">
    <a onclick='scrollBottom()'>Jump to end</a>
  </li>
  <li class="active" style="float: right">
    <a onclick='toggleWrap()'>Toggle wrap</a>
  </li>

</ul>
<div class="tab-content">
  {% for log in logs %}
  <div role="tabpanel" class="tab-pane {{ 'active' if loop.last else '' }}" id="{{ loop.index }}">
    <img id="loading-{{ loop.index }}" style="margin-top:0%; margin-left:50%; height:50px; width:50px; position: absolute;"
         alt="spinner" src="{{ url_for('static', filename='loading.gif') }}">
    <pre><code id="try-{{ loop.index }}" class="{{ 'wrap' if wrapped else '' }}">{{ log }}</code></pre>
  </div>
  {% endfor %}
  </div>
{% endblock %}
{% block tail %}
{{ super() }}
<script>
    // Time interval to wait before next log fetching. Default 2s.
    const DELAY = "{{ (log_fetch_delay_sec | int ) * 1000 }}";
    // Distance away from page bottom to enable auto tailing.
    const AUTO_TAILING_OFFSET = "{{ log_auto_tailing_offset | int }}";
    // Animation speed for auto tailing log display.
    const ANIMATION_SPEED = "{{ log_animation_speed | int }}";
    // Total number of tabs to show.
    const TOTAL_ATTEMPTS = "{{ logs|length }}";

    // Recursively fetch logs from flask endpoint.
    function recurse(delay=DELAY) {
      return new Promise((resolve) => setTimeout(resolve, delay));
    }

    // Enable auto tailing only when users scroll down to the bottom
    // of the page. This prevent auto tailing the page if users want
    // to view earlier rendered messages.
    function checkAutoTailingCondition() {
      const docHeight = $(document).height();
      console.debug($(window).scrollTop())
      console.debug($(window).height())
      console.debug($(document).height())
      return $(window).scrollTop() != 0
             && ($(window).scrollTop() + $(window).height() > docHeight - AUTO_TAILING_OFFSET);
    }

    function toggleWrap() {
      $("pre code").toggleClass("wrap")
    }

    function scrollBottom() {
      $("html, body").animate({ scrollTop: $(document).height() }, ANIMATION_SPEED);
    }

    // Streaming log with auto-tailing.
    function autoTailingLog(try_number, metadata=null, auto_tailing=false) {
      console.debug("Auto-tailing log for dag_id: {{ dag_id }}, task_id: {{ task_id }}, \
       execution_date: {{ execution_date }}, try_number: " + try_number + ", metadata: " + JSON.stringify(metadata));

      return Promise.resolve(
        $.ajax({
          url: "{{ url_for("airflow.get_logs_with_metadata") }}",
          data: {
            dag_id: "{{ dag_id }}",
            task_id: "{{ task_id }}",
            execution_date: "{{ execution_date }}",
            try_number: try_number,
            metadata: JSON.stringify(metadata),
          },
        })).then(res => {
          // Stop recursive call to backend when error occurs.
          if (!res) {
            document.getElementById("loading-"+try_number).style.display = "none";
            return;
          }
          // res.error is a boolean
          // res.message is the log itself or the error message
          if (res.error) {
            if (res.message) {
              console.error("Error while retrieving log: " + res.message);
            }
            document.getElementById("loading-"+try_number).style.display = "none";
            return;
          }

          if (res.message) {
            // Auto scroll window to the end if current window location is near the end.
            if(auto_tailing && checkAutoTailingCondition()) {
              var should_scroll = true
            }
            // The message may contain HTML, so either have to escape it or write it as text.
            document.getElementById(`try-${try_number}`).textContent += res.message + "\n";
            // Auto scroll window to the end if current window location is near the end.
            if(should_scroll) {
              scrollBottom();
            }
          }

          if (res.metadata.end_of_log) {
            document.getElementById("loading-"+try_number).style.display = "none";
            return;
          }
          return recurse().then(() => autoTailingLog(
            try_number, res.metadata, auto_tailing));
        });
    }
    $(document).ready(function() {
      // Lazily load all past task instance logs.
      // TODO: We only need to have recursive queries for
      // latest running task instances. Currently it does not
      // work well with ElasticSearch because ES query only
      // returns at most 10k documents. We want the ability
      // to display all logs in the front-end.
      // An optimization here is to render from latest attempt.
      for(let i = TOTAL_ATTEMPTS; i >= 1; i--) {
        // Only auto_tailing the page when streaming the latest attempt.
        autoTailingLog(i, null, auto_tailing=(i == TOTAL_ATTEMPTS));
      }
    });

</script>
{% endblock %}
